using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Spike.ContractBuilder;

namespace Spike.TestHarness
{
    public partial class NewTestCaseDialog : Form
    {
        public NewTestCaseDialog()
        {
            InitializeComponent();
        }

        public void SetCollection(ServiceClientContract[] contracts, List<string> existingTestCases)
        {            
            cboContracts.Items.AddRange(contracts);
            cboContracts.DisplayMember = "Name";
            cboEndpoint.Items.Clear();
            cboMethod.Items.Clear();

            _existingTestCases = existingTestCases;
        }

        private List<string> _existingTestCases = new List<string>();

        public string TestName
        {
            get { return txtName.Text; }
        }

        public string Contract
        {
            get { return cboContracts.Text; }
        }

        public string Endpoint
        {
            get { return cboEndpoint.Text; }
        }

        public string Method
        {
            get { return cboMethod.Text; }
        }

        public string Assembly
        {
            get { return cboAssembly.Text; }
        }

        private string _uri;

        public string URI
        {
            get { return _uri; }
            set { _uri = value; }
        }
	

        private void cboGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            ServiceClientContract contract = cboContracts.SelectedItem as ServiceClientContract;
            
            cboEndpoint.Items.Clear();
            cboEndpoint.Items.AddRange(contract.GetEndpoints());
            cboEndpoint.DisplayMember ="Name";

            if (cboEndpoint.Items.Count == 1)
            {
                cboEndpoint.SelectedIndex = 0;
                cboEndpoint.Enabled = false;
            }
            else
                cboEndpoint.Enabled = true;
        }

        

        private void cboMethod_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        

        private void cboAssembly_SelectedIndexChanged(object sender, EventArgs e)
        {
            string assembly = cboAssembly.Text;
            ClientEndpoint endpoint = cboEndpoint.SelectedItem as ClientEndpoint;

            cboMethod.Items.Clear();
            cboMethod.Items.AddRange(ContractGenerator.Instance.GetMethods(assembly, this.Contract, this.Endpoint));

            if (cboMethod.Items.Count == 1)
            {
                cboMethod.SelectedIndex = 0;
                cboMethod.Enabled = false;
            }
            else
                cboMethod.Enabled = true;
        }

        private void cboEndpoint_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClientEndpoint endpoint = cboEndpoint.SelectedItem as ClientEndpoint;
            _uri = endpoint.URI;
            cboAssembly.Items.Clear();
            cboAssembly.Items.AddRange(ContractGenerator.Instance.DetermineAssembly(endpoint.Contract).ToArray());

            if (cboAssembly.Items.Count == 1)
            {
                cboAssembly.SelectedIndex = 0;
                cboAssembly.Enabled = false;
            }
            else
                cboAssembly.Enabled = true;

        }

        #region Validation
        private void cboMethod_Validating(object sender, CancelEventArgs e)
        {
            if (cboMethod.Text.Trim() == string.Empty)
            {
                errorProvider1.SetError(cboMethod, "Please select a method.");
                e.Cancel = true;
            }
            else
                errorProvider1.SetError(cboMethod, "");
        }
        private void cboContract_Validating(object sender, CancelEventArgs e)
        {
            if (cboEndpoint.Text.Trim() == string.Empty)
            {
                errorProvider1.SetError(cboEndpoint, "Please select an endpoint.");
                e.Cancel = true;
            }
            else
                errorProvider1.SetError(cboEndpoint, "");
        }
        private void txtName_Validating(object sender, CancelEventArgs e)
        {
            if (txtName.Text.Trim() == string.Empty)
            {
                errorProvider1.SetError(txtName, "Please enter a Name for the Test Case.");
                e.Cancel = true;
            }
            else
            {
                if (_existingTestCases.Contains(txtName.Text))
                {
                    errorProvider1.SetError(txtName, "Please enter a Name for the Test Case." + Environment.NewLine +"This name has already been assigned.");
                    e.Cancel = true;
                }
                else
                    errorProvider1.SetError(txtName, "");
            }
        }

        private void cboService_Validating(object sender, CancelEventArgs e)
        {
            if (cboContracts.Text.Trim() == string.Empty)
            {
                errorProvider1.SetError(cboContracts, "Please select a group.");
                e.Cancel = true;
            }
            else
                errorProvider1.SetError(cboContracts, "");
        }

        #endregion
    }
}